/**
 *
 */
package susano.demo.action.company;

import java.text.SimpleDateFormat;
import java.util.Date;

import susano.common.util.BeanMap;
import susano.demo.service.company.CompanyService;
import susano.presen.controller.Action;
import susano.presen.controller.Navigation;
import susano.presen.threadlocal.DateFormatLocal;
import susano.presen.validator.Validators;

/**
 * @author tanaka.akira
 *
 */
public class AddAction extends Action {

	protected CompanyService companyService = new CompanyService();

	private static final String DATE_FORMAT = "yyyy/MM/dd";

	/*
	 * (non-Javadoc)
	 *
	 * @see susano.presen.controller.Action#execute()
	 */
	@Override
	protected Navigation execute() {

		SimpleDateFormat sdf = DateFormatLocal.get(DATE_FORMAT);

		BeanMap company = new BeanMap();
		company.put("name", asString("companyName"));
		company.put("date", sdf.format(new Date()));

		// 保存
		if (!companyService.put(company)) {
			errors.put("company", "会社情報の保存に失敗しました。");
			return forward("index.jsp");
		}

		return redirect("/company/index");
	}

	/*
	 * (non-Javadoc)
	 *
	 * @see susano.presen.controller.Action#init()
	 */
	@Override
	protected Navigation init() {
		return forward("index.jsp");
	}

	/*
	 * (non-Javadoc)
	 *
	 * @see susano.presen.controller.Action#validate()
	 */
	@Override
	protected Navigation validate() {
		Validators v = new Validators(request);
		v.add("companyName", v.required());
		v.validate();
		return forward("index.jsp");
	}

}
